<?php

declare(strict_types=1);

namespace App\Command;

use App\Modules\ImportDorisData\Factory\ImportDorisFactory;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use Swoole\Coroutine;
use Swoole\Database\PDOConfig;
use Swoole\Database\PDOPool;
use Swoole\Runtime;
use function Swoole\Coroutine\run;

/**
 * @Command
 */
#[Command]
class ImportDorisDataComamnd extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('app:import-doris');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('Hyperf Demo Command');
    }

    /**
     * php74 bin/hyperf.php app:import-doris
     * @return void
     */
    public function handle()
    {
        $tables = ['call_task', 'call_result', 'call_log', 'call_detail'];
//        $tables = ['call_detail'];
        $pool = new PDOPool((new PDOConfig())
            ->withHost(config('databases.doris.host'))
            ->withPort(intval(config('databases.doris.port')))
            // ->withUnixSocket('/tmp/mysql.sock')
            ->withDbName(config('databases.doris.database'))
            ->withUsername(config('databases.doris.username'))
            ->withPassword(config('databases.doris.password')));

        foreach ($tables as $table) {
            for ($i = 1; $i <= 10; $i++) {
                co(function () use ($table, $pool) {
                    $table = new ImportDorisFactory($table);
                    $table->importData($pool);
                });
            }
        }
    }
}


